package com;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import javax.swing.JOptionPane;

public class ClientThreed implements Runnable{
	private Socket socket;
	private MainFrame mf;
	private ObjectInputStream ois;
	public ClientThreed(MainFrame mainFrame) {
		this.mf = mainFrame;
	}
	@Override
	public void run() {
		while(true){
			socket=mf.getSocket();
			if(socket!=null){
				try {					
					ois=new ObjectInputStream(socket.getInputStream());
					Object message=ois.readObject();
					if(message instanceof Integer){
						int code=(int) message;
						if(code<5){
						  LoginPanel loginPanel=(LoginPanel) mf.getRootPane().getGlassPane();
						  loginPanel.agreeState=(Integer) message;
					      loginPanel.dialog.setVisible(false);
						  loginPanel.dialog.dispose();
						}
						else{
							switch(code){
							case ChessPanel.OPRATION_START:
								
								if(mf.getChessPanel().getGridPanel().isStart())
								{						  
								  mf.send(mf.getSocket(), ChessPanel.OPRATION_ACCEPT_START);
								  mf.getChessPanel().getGridPanel().setOpponentStart(true);
								  mf.getChessPanel().repaint();
								}
								else{
									mf.appendMessage(mf.opponent.getName()+"请求开始...\n");
								}
								break;
							case ChessPanel.OPRATION_ACCEPT_START:
								mf.getChessPanel().getGridPanel().setOpponentStart(true);
								break;
							case ChessPanel.OPRATION_GIVEUP:
								int f=JOptionPane.showConfirmDialog(mf, mf.opponent.getName()+"投降，同意吗", "信息", JOptionPane.OK_CANCEL_OPTION);
								if(f==JOptionPane.OK_OPTION){
									mf.getChessPanel().reInit();
									mf.getChessPanel().getGridPanel().setWin(true);
									mf.getChessPanel().repaint();
									mf.send(mf.getSocket(), ChessPanel.OPRATION_ACCEPT_GIVEUP);
								}
								else if(f==JOptionPane.CANCEL_OPTION){
									mf.send(mf.getSocket(), ChessPanel.OPRATION_REFUSE_GIVEUP);
								}
								break;
							case ChessPanel.OPRATION_ACCEPT_GIVEUP:
								mf.getChessPanel().reInit();
								mf.getChessPanel().getGridPanel().setWin(false);
								mf.getChessPanel().repaint();
								break;
							case ChessPanel.OPRATION_REFUSE_GIVEUP:
								JOptionPane.showMessageDialog(mf, mf.opponent.getName()+"拒绝投降！");
								break;
							}							
						}
					}
					if(message instanceof User){
						User user=(User) message;
						user.setOpponent(true);
						mf.setUser(user);
					}
					if(message instanceof Chess){
						Chess chess=(Chess) message;
						ChessArray ca=ChessArray.getInstance();
						ca.setChess(chess);
						mf.getChessPanel().getGridPanel().setMy(true);
					}
					if(message instanceof String){
						mf.appendMessage((String)message);
					}
				} catch (IOException e) {
					mf.setSocket(null);
					mf.isLinked=false;
					mf.getChessPanel().reInit();
					mf.getChessPanel().repaint();
					JOptionPane.showMessageDialog(mf, "连接中断");
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
